﻿var pageIndex = 1, curRoleId = 0;
$(function () {
    layui.use('table', function () {
        var $ = layui.$, table = layui.table;
        var tblList = table.render({
            elem: '#tblList'
            , url: '/gx/sysrole/getlist' + getSearchKeys()
            , toolbar: '#toolbarList'
            , defaultToolbar: ['filter', 'print']//['filter', 'print', 'exports']
            , cellMinWidth: 150
            , page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']  //自定义分页布局
                , curr: 1                                                   //当前页码
                , groups: 9                                                 //显示页码数
                , first: false                                              //不显示首页
                , last: false                                               //不显示尾页
            }
            , cols: [[
                { type: 'checkbox' },
                { field: 'Name', title: '角色名称' }
            ]]
        });
        //监听点击行事件
        table.on('row(tblList)', function (obj) {
            var data = obj.data;
            curRoleId = data.Id;
            //标注选中样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            //$("input[name='layTableCheckbox']").prop("checked", false);
            //$(".layui-table-body .layui-form-checkbox").removeClass("layui-form-checked");
            //obj.tr.find("input[name='layTableCheckbox']").prop("checked", true).siblings(".layui-form-checkbox").addClass("layui-form-checked");
            $(".layui-collapse .layui-form-checkbox").removeClass("layui-form-checked");
            $("input[name='mcbox'],input[name='bcbox']").prop("checked", false);
            $.post("/gx/sysrole/getrolepower", { id: data.Id }, function (res) {
                $(res).each(function (i, item) {
                    if (item.PowerType == 1) {
                        $("input[name='mcbox'][value='" + item.FK_PowerId + "']").prop("checked", true).siblings(".layui-form-checkbox").addClass("layui-form-checked");
                    } else {
                        $("input[name='bcbox'][value='" + item.FK_PowerId + "']").prop("checked", true).siblings(".layui-form-checkbox").addClass("layui-form-checked");
                    }
                });
            })
        });
        //头工具栏事件
        table.on('toolbar(tblList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            switch (obj.event) {
                case 'add':
                    win.open("/gx/sysrole/addedit", 450, 350, "新增");
                    break;
                case 'edit':
                    if (data != "") {
                        win.open("/gx/sysrole/addedit?id=" + data[0].Id, 450, 350, "编辑");
                    } else {
                        win.info("请选择要编辑的角色");
                    }
                    break;
                case 'del':
                    if (data != "") {
                        var id = "";
                        $(data).each(function (i, item) {
                            id += item.Id + ",";
                        });
                        id = id.substring(0, id.length - 1);
                        win.confirm("确认要删除吗？", function () {
                            $.post("/gx/sysrole/deletedata", { id: id }, function (res) {
                                if (res.Code == 200) {
                                    win.success("删除成功");
                                    getList();
                                } else {
                                    win.alert(res.Message);
                                }
                            });
                        });
                    } else {
                        win.info("请选择要删除的角色");
                    }
                    break;
            };
        });
        var active = {
            search: function () {
                //执行搜索并重载角色
                tblList.reload({
                    url: "/gx/sysrole/getlist" + getSearchKeys(),
                    page: {
                        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']  //自定义分页布局
                        , curr: 1                                                   //当前页码
                        , groups: 9                                                 //显示页码数
                        , first: false                                              //不显示首页
                        , last: false                                               //不显示尾页
                    }
                });
            }
        };
        //搜索按钮
        $('#btnSearch').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
});

//外部搜索
function getList() {
    $('#btnSearch').click();
}
//获取查询条件
function getSearchKeys() {
    var searchKeys = "";
    $(".layui-elem-field input").each(function (i, item) {
        searchKeys += $(item).attr("name") + "=" + $(item).val() + "&";
    });
    if (searchKeys != "") searchKeys = "?" + searchKeys.substring(0, searchKeys.length - 1);
    return searchKeys;
}
//父级展开
function onOpen(obj, id) {
    if ($(obj).siblings("i").hasClass("layui-icon-right")) {
        $(obj).siblings("i").removeClass("layui-icon-right").addClass("layui-icon-down");
        $("#nodeContent" + id).addClass("layui-show");
    } else {
        $(obj).siblings("i").removeClass("layui-icon-down").addClass("layui-icon-right");
        $("#nodeContent" + id).removeClass("layui-show");
    }
}
//保存角色
function save() {
    if (curRoleId > 0) {
        var menuIds = win.getChecked($("input[name='mcbox']:checked"));
        var buttonIds = win.getChecked($("input[name='bcbox']:checked"));
        win.showLoading("正在保存...");
        $.post("/gx/sysrole/SaveRolePower", { id: curRoleId, menuIds: menuIds, buttonIds: buttonIds }, function (res) {
            win.hideLoading();
            if (res.Code == 200) {
                win.success("保存成功");
            } else {
                win.alert("保存失败");
            }
        });
    }
}